package com.wangcang.widgetlibrary.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.wangcang.widgetlibrary.R;
import com.wangcang.widgetlibrary.activity.base.BaseActivity;
import com.wei.android.lib.fingerprintidentify.FingerprintIdentify;
import com.wei.android.lib.fingerprintidentify.base.BaseFingerprint;


public class FingerActivity extends BaseActivity {
    Button mStartBt = null; // 开始进行指纹识别
    TextView mTextV = null;
    FingerprintIdentify mFingerprintIdentify = null; // 指纹识别类，所有接口都封装在这个类里面
    int MAX_AVAILABLE_TIMES = 5; // 识别10次就自动结束识别
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_finger);

        mStartBt = findView(R.id.start);
        mTextV = findView(R.id.text);

        mFingerprintIdentify = new FingerprintIdentify(getApplicationContext(), new BaseFingerprint.FingerprintIdentifyExceptionListener() {
            @Override
            public void onCatchException(Throwable exception) {
                append("\nException：" + exception.getLocalizedMessage());
            }
        });
//        append("\nisHardwareEnable() " + mFingerprintIdentify.isHardwareEnable()); // 判断是否硬件支持指纹识别
//        append("\nisRegisteredFingerprint() " + mFingerprintIdentify.isRegisteredFingerprint()); // 判断手机是否已经录入指纹
//        append("\nisFingerprintEnable() " + mFingerprintIdentify.isFingerprintEnable()); // 判断手机硬件是否支持指纹识别并且已经录入指纹

        if (mFingerprintIdentify.isHardwareEnable()) {
            append("手机硬件硬件支持指纹识别");

            if (mFingerprintIdentify.isRegisteredFingerprint()) {
                append("手机已经录入指纹");
            }
            else {
                append("手机没有录入指纹");
            }
        }
        else {
            append("硬件不支持指纹识别");
        }

        if (!mFingerprintIdentify.isFingerprintEnable()) {
            append("\n" + "当前手机不支持指纹识别");
            return;
        }

        append("\n" + "点击开始按钮进行指纹识别");

        mStartBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                append("\n" + "开始进行指纹识别，请放置你的指纹");

                mFingerprintIdentify.startIdentify(MAX_AVAILABLE_TIMES, new BaseFingerprint.FingerprintIdentifyListener() {
                    @Override
                    public void onSucceed() {
                        append("\n" + "指纹识别成功");
                    }

                    @Override
                    public void onNotMatch(int availableTimes) {
                        append("\n" + "指纹不匹配，剩余" + availableTimes + "次机会");
                    }

                    @Override
                    public void onFailed(boolean isDeviceLocked) {
                        append("\n" + "指纹识别失败");
                    }

                    @Override
                    public void onStartFailedByDeviceLocked() {
                        append("\n" + "超过识别次数，请重新打开app");
                    }
                });
            }
        });
    }

    private void append(String msg) {
        mTextV.append(msg);
    }

    @Override
    public void onPause() {
        super.onPause();
        mFingerprintIdentify.cancelIdentify();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mFingerprintIdentify.cancelIdentify();
    }
}
